package org.loofer.uikit;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import java.util.HashSet;

public class GridMenu extends Dialog {
    private static final int DEFAULT_NUM_COLUMNS = 4;
    private static final int DEFAULT_WINDOW_COLOR = 1711276032;
    private OnClickListener mClickListener;
    private ViewGroup mContentView;
    private int mGravity;
    private CustomGridLayout mGridLayout;
    private OnItemClickListener mItemClickListener;
    private final HashSet<Integer> mItemIds;
    private Mode mMode;
    private int mNumColumns;
    private final HashSet<Integer> mVisibleItemIds;
    private ViewGroup mWindowView;

    protected interface Item {
        View getView();

        void setIcon(Drawable drawable);

        void setTitle(CharSequence charSequence);
    }

    private static class ItemView extends LinearLayout implements Item {
        private ImageView mIconView;
        private TextView mTitleView;

        ItemView(Context context) {
            super(context);
            setOrientation(1);
            init();
        }

        private void init() {
            ImageView iconView = new ImageView(getContext());
            LayoutParams iconParams = new LayoutParams(-2, -2);
            iconParams.gravity = 1;
            addView(iconView, iconParams);
            TextView titleView = new TextView(getContext());
            LayoutParams titleParams = new LayoutParams(-2, -2);
            titleParams.gravity = 1;
            addView(titleView, titleParams);
            this.mIconView = iconView;
            this.mTitleView = titleView;
        }

        public View getView() {
            return this;
        }

        public void setIcon(Drawable drawable) {
            this.mIconView.setImageDrawable(drawable);
        }

        public void setTitle(CharSequence title) {
            this.mTitleView.setText(title);
        }
    }

    public enum Mode {
        NO_STRETCH,
        AUTO_FIT
    }

    public interface OnItemClickListener {
        boolean onItemClick(GridMenu gridMenu, int i);
    }

    public GridMenu(Context context) {
        this(context, 16973840);
    }

    protected GridMenu(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mClickListener = new OnClickListener() {
            public void onClick(View v) {
                if (GridMenu.this.performItemClick(v)) {
                    GridMenu.this.dismiss();
                }
            }
        };
        this.mMode = Mode.AUTO_FIT;
        this.mGravity = 80;
        this.mNumColumns = 4;
        this.mItemIds = new HashSet();
        this.mVisibleItemIds = new HashSet();
        init();
    }

    public GridMenu(Context context, int theme) {
        super(context, theme);
        this.mClickListener = /* anonymous class already generated */;
        this.mMode = Mode.AUTO_FIT;
        this.mGravity = 80;
        this.mNumColumns = 4;
        this.mItemIds = new HashSet();
        this.mVisibleItemIds = new HashSet();
        init();
    }

    @SuppressLint({"InlinedApi"})
    private void init() {
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        this.mContentView = findContentView();
        this.mGridLayout = findGridLayout();
        this.mGridLayout.setStretchMode(2);
        this.mWindowView = new FrameLayout(getContext());
        this.mWindowView.setBackgroundColor(DEFAULT_WINDOW_COLOR);
        this.mWindowView.addView(this.mContentView, new FrameLayout.LayoutParams(-1, -2, this.mGravity));
        setContentView(this.mWindowView);
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != 0) {
            return super.onTouchEvent(event);
        }
        cancel();
        return true;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == 82) {
            dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }

    public void add(int id, int titleRes, int iconRes) {
        performAdd(id, titleRes, iconRes);
    }

    public void add(int id, CharSequence title, int iconRes) {
        performAdd(id, title, iconRes);
    }

    protected Item performAdd(int id, int titleRes, int iconRes) {
        return performAdd(id, getContext().getResources().getString(titleRes), iconRes);
    }

    protected Item performAdd(int id, CharSequence title, int iconRes) {
        if (this.mItemIds.contains(Integer.valueOf(id))) {
            throw new RuntimeException("item already added with id " + id);
        }
        this.mItemIds.add(Integer.valueOf(id));
        this.mVisibleItemIds.add(Integer.valueOf(id));
        Item item = generateItem();
        item.getView().setId(id);
        item.setTitle(title);
        item.setIcon(getContext().getResources().getDrawable(iconRes));
        item.getView().setOnClickListener(this.mClickListener);
        this.mGridLayout.addView(item.getView(), -1, -2);
        this.mGridLayout.setNumColumns(calculateNumColumns());
        return item;
    }

    public void remove(int id) {
        performRemove(id);
    }

    protected void performRemove(int id) {
        if (this.mItemIds.remove(Integer.valueOf(id))) {
            this.mVisibleItemIds.remove(Integer.valueOf(id));
            View itemView = this.mGridLayout.findViewById(id);
            if (itemView != null) {
                this.mGridLayout.removeView(itemView);
                this.mGridLayout.setNumColumns(calculateNumColumns());
            }
        }
    }

    public void setVisible(int id, boolean visible) {
        if (this.mItemIds.contains(Integer.valueOf(id))) {
            if (visible) {
                this.mVisibleItemIds.add(Integer.valueOf(id));
            } else {
                this.mVisibleItemIds.remove(Integer.valueOf(id));
            }
            this.mGridLayout.findViewById(id).setVisibility(visible ? 0 : 8);
            this.mGridLayout.setNumColumns(calculateNumColumns());
        }
    }

    public void setNumColumns(int numColumns) {
        if (this.mNumColumns != numColumns) {
            this.mNumColumns = numColumns;
            this.mGridLayout.setNumColumns(calculateNumColumns());
        }
    }

    private int calculateNumColumns() {
        int num = this.mNumColumns;
        switch (this.mMode) {
            case AUTO_FIT:
                return Math.min(this.mNumColumns, getVisibleCount());
            case NO_STRETCH:
                return this.mNumColumns;
            default:
                return num;
        }
    }

    public void setMode(Mode mode) {
        if (this.mMode != mode) {
            this.mMode = mode;
            this.mGridLayout.setNumColumns(calculateNumColumns());
        }
    }

    public void setGravity(int gravity) {
        if (this.mGravity != gravity) {
            this.mGravity = gravity;
            FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams) this.mContentView.getLayoutParams();
            contentParams.gravity = gravity;
            this.mContentView.setLayoutParams(contentParams);
        }
    }

    public void setWindowBackground(Drawable background) {
        this.mWindowView.setBackgroundDrawable(background);
    }

    public void setBackground(Drawable background) {
        this.mContentView.setBackgroundDrawable(background);
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;
    }

    public int getCount() {
        return this.mItemIds.size();
    }

    public int getVisibleCount() {
        return this.mVisibleItemIds.size();
    }

    private boolean performItemClick(View itemView) {
        OnItemClickListener itemClickListener = this.mItemClickListener;
        if (itemClickListener != null) {
            return itemClickListener.onItemClick(this, itemView.getId());
        }
        return false;
    }

    protected ViewGroup findContentView() {
        if (this.mContentView != null) {
            return this.mContentView;
        }
        ViewGroup contentView = new LinearLayout(getContext());
        contentView.setOrientation(1);
        return contentView;
    }

    protected CustomGridLayout findGridLayout() {
        if (this.mGridLayout != null) {
            return this.mGridLayout;
        }
        CustomGridLayout gridLayout = new CustomGridLayout(getContext());
        this.mContentView.addView(gridLayout, -1, -2);
        return gridLayout;
    }

    protected Item generateItem() {
        return new ItemView(getContext());
    }
}
